package json

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

class JsonTest {
    String name
    int age
}

def list1 = [new JsonTest(name: 'cherry', age: 30), new JsonTest(name: 'anne', age: 20)]
//转json字符串
def jsonTest = new JsonOutput().toJson(list1)
println jsonTest

//转实体bean
List jt = new JsonSlurper().parseText(jsonTest)
println jt[0].name

def response = getNetworkResponse('https://httpbin.org/get')

println response.headers.Accept

def getNetworkResponse(String url){

    def connection = new URL(url).openConnection()
    connection.setRequestMethod("GET")
    connection.connect()
    def response = connection.content.text
    println response
    //json转实体
    new JsonSlurper().parseText(response)
}

